package supermarket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

/*
 * 客户端的显示界面
 * */
public class Client {
    int point = 2000;
    // 定义扫描器,用于键盘输入
    Scanner input = new Scanner(System.in);
    // 创建客户端业务类对象,调用方法
    public static ClientAsk ca = new ClientAsk();

    // 输出端
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("***********欢迎使用华联超市系统*************");
            System.out.println("1.登录" + "\n2.退出");
            System.out.println("***********请选择1/2*************");
            String num = input.next();
            if (num.equals("1")) {
                // 登录视图
                Client client = new Client();
                client.createLoginView();
            } else if (num.equals("2")) {
                // 退出系统
                System.exit(0);
            } else {
                // 输入有误,报错
                System.out.println("无此选项!");
            }
        }
    }

    // 登录视图 通过输入的用户名与密码验证用户身份
    public void createLoginView() {
        while (true) {
            // 提示登录信息
            System.out.print("请输入用户名:");
            String name = input.next();
            System.out.print("请输入密码:");
            String password = input.next();
            // 调用客户端登录方法,接收返回值,判断登录者的身份
            DataPool datas = ca.login(name, password);
            // 判断是管理员还是收银员
            if (datas.getFlag().equals(Flag.REPERTORY)) {
                RepertoryMgr();
                break;
            } else if (datas.getFlag().equals(Flag.CASHIER)) {
                CashierMgr();
                break;
            } else {
                System.out.println("用户名或密码有误,请重新输入");
            }
        }
    }

    // 管理员界面
    public void RepertoryMgr() {
        System.out.println("********************************欢迎登录华联超市库存管理系统********************************");
        while (true) {
            System.out.println("请选择进行的操作：1.商品入库  2.商品出库   3.新增商品  4.查询全部商品  5.按查询号查询商品  6.退出");
            int num = input.nextInt();
            switch (num) {
                // 商品入库
                case 1:
                    addin();
                    break;
                // 商品出库
                case 2:
                    out();
                    break;
                case 3:
                    newAdd();
                    break;
                case 4:
                    allGoods();
                    break;
                case 5:
                    oneGoods();
                    break;
                case 6:
                    return;

            }
        }
    }

    // 收银员界面
    public void CashierMgr() {
        System.out.println("***********************欢迎登录华联超市收银系统**************************");
        while (true) {
            System.out.println("请选择进行的操作：1.扫描商品 2.修改数量 3.结账 4.退出");
            int num = input.nextInt();
            switch (num) {
                case 1:
                    scan();
                    break;
                case 2:
                    modify();
                    break;
                case 3:
                    accounts();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("没有此选项");
            }
        }
    }

    // 扫描商品编号
    public void scan() {
        System.out.print("请输入要扫描的商品编号：");
        String number = input.next();
        // 调用客户端扫描方法，判断是否有此商品
        DataPool data = ca.scan(number);
        if (data.getFlag().equals(Flag.SCAN)) {
            System.out.println("扫描成功！");
            System.out.println("商品名称\t\t" + "\t数量" + "\t单价" + "\t金额");
            System.out.println("---------------------------------------------------------------------");
            for (Commodity com : data.getBuyList()) {
                System.out.println("(" + com.getGoodNum() + ")" + com.getGoodName() + com.getGoodVolume() + "\t\t"
                        + com.getGoodCount() + "\t" + com.getGoodprice() + "\t" + com.getGoodprice());
            }
            System.out.println("---------------------------------------------------------------------");
        } else {
            System.out.println("对不起，没有此商品，扫描失败");
        }
    }

    // 修改商品数量
    public void modify() {
        int num = -1;
        // 判断是否有购买此商品
        while (true) {
            Scanner inp = new Scanner(System.in);
            System.out.print("请输入要修改的商品编号");
            String number = input.next();
            // 修改数量
            while (true) {
                try {
                    System.out.print("请输入修改数量：");
                    num = inp.nextInt();
                    break;
                } catch (Exception e) {
                    System.out.println("请输入整数");
                    continue;
                }
            }
            DataPool data = ca.modify(number, num);
            // 有此商品时
            if (data.getFlag().equals(Flag.MODIFY)) {
                // 输出所有购买的商品
                System.out.println("商品名称\t\t\t数量" + "\t单价" + "\t金额");
                System.out.println("---------------------------------------------------------------------");
                for (Commodity com : data.getBuyList()) {
                    System.out.println("(" + com.getGoodNum() + ")" + com.getGoodName() + com.getGoodVolume() + "\t\t"
                            + com.getGoodCount() + "\t" + com.getGoodprice() + "\t"
                            + com.getGoodprice() * com.getGoodCount());
                }
                System.out.println("---------------------------------------------------------------------");
                break;

            } else {
                System.out.println("没有扫描此商品");
            }
        }
    }

    // 结账
    public void accounts() {
        // 得到购买商品的总金额
        Double money = 0.0;
        DataPool datas = ca.accounts();
        if (datas.getFlag().equals(Flag.ACCOUNT)) {
            for (Commodity comm : datas.getBuyList()) {
                money = comm.getGoodprice()*comm.getGoodCount() + money;
            }
            System.out.println("总金额是：" + money);
            while(true){
                // 选择结账方式
                System.out.println("1:普通结账 2：会员结账");
                int num = input.nextInt();
                switch (num) {
                    case 1:
                        // 判断输入付款金额是否正确
                        while (true) {
                            Double pay = -1.0;
                            try {
                                System.out.print("请输入你要支付的金额：");
                                pay = input.nextDouble();
                            } catch (Exception e) {
                                System.out.println("请输入正确格式的金额");
                                continue;
                            }
                            if (pay > 0) {
                                System.out.println("\t\t\t\t华联超市");
                                System.out.println("收银员:1001\t小票号:" + ticket());
                                // 显示购买列表
                                System.out.println("序\t商品名称(单位)\t\t\t数量\t\t单价\t\t金额");
                                System.out.println(
                                        "------------------------------------------------------------------------------------------------");
                                int i = 0;
                                for (Commodity goods : datas.getBuyList()) {
                                    i = i + 1;
                                    System.out.println(i + "\t(" + goods.getGoodNum() + ")" + goods.getGoodName()
                                            + goods.getGoodVolume() + "\t\t" + goods.getGoodCount() + "\t\t"
                                            + goods.getGoodprice() +"\t\t"+ (goods.getGoodCount() * goods.getGoodprice()));
                                }
                                System.out.println(
                                        "------------------------------------------------------------------------------------------------");
                                // 显示购买金额和购买的数量,结账提示
                                int amount = 0;
                                for (Commodity goods : datas.getBuyList()) {
                                    amount += goods.getGoodCount();
                                }
                                System.out.println("总数量:" + amount + "\t\t应收" + money);
                                System.out.println("实收: " + pay + "\t\t找零: " + (pay - money));
                                System.out.println(Date());
                                System.out.println("此小票是您退换货及核对积分的凭证,请妥善保存!");
                                System.out.println(
                                        "***************************************************************************************************");
                                // 购买列表清空
                                ca.clear();
                                break;
                            } else {
                                System.out.println("请输入正确的金额！");
                            }
                        }
                        return;
                    case 2:
                        vipPay(money);
                        return;
                    default:
                        System.out.println("没有此选项");
                        break;
                }
            }
        } else {
            System.out.println("没有购买任何商品");
        }
    }

    /**
     * 得到小票号的方法.日期+(1-1000)的随机数
     */
    private String ticket() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
        String ticket = sdf.format(date) + (int) (Math.random() * 1000);
        return ticket;

    }

    /**
     * 得到日期的方法,在结账时显示日期用
     */
    // 显示日期的方法
    private String Date() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/   hh:mm:ss");
        String paydate = sdf.format(date);
        return paydate;
    }

    // vip的结账方法
    public void vipPay(Double money) {
        while (true) {
            // 判断会员卡号是否存在
            System.out.print("请输入你的会员卡号：");
            String cardNum = input.next();
            DataPool data = ca.vipPay(cardNum);
            if (data.getFlag().equals(Flag.VIPNUM)) {
                // 判断输入金额是否正确
                Double pay = -1.0;
                while (true) {
                    try {
                        Scanner inpu = new Scanner(System.in);
                        System.out.println("会员卡号正确，请输入你要支付的金额：");
                        pay = inpu.nextDouble();
                    } catch (Exception e) {
                        System.out.println("请输入正确格式的金额");
                        continue;
                    }
                    if (pay > 0) {
                        System.out.println("\t\t\t\t华联超市");
                        System.out.println("收银员:1001\t小票号:" + ticket());
                        // 显示购买列表
                        System.out.println("序\t商品名称(单位)\t\t\t数量\t\t单价\t\t金额");
                        System.out.println(
                                "------------------------------------------------------------------------------------------------");
                        int i = 0;
                        for (Commodity goods : data.getBuyList()) {
                            i = i + 1;
                            System.out.println(i + "\t(" + goods.getGoodNum() + ")" + goods.getGoodName()
                                    + goods.getGoodVolume() + "\t\t" + goods.getGoodCount() + "\t\t"
                                    + goods.getGoodprice() + "\t\t"+(goods.getGoodCount() * goods.getGoodprice()));
                        }
                        System.out.println(
                                "------------------------------------------------------------------------------------------------");
                        // 显示购买金额和购买的数量,结账提示
                        int amount = 0;
                        for (Commodity goods : data.getBuyList()) {
                            amount += goods.getGoodCount();
                        }
                        System.out.println("总数量:" + amount + "\t\t应收" + money);
                        System.out.println("实收: " + pay + "\t\t找零: " + (pay - money));
                        // Vip积分提示
                        System.out.println("本次累计积分成功\t\t卡号:2011");
                        System.out.println("消费前积分  + 本次消费积分  = 消费后积分");
                        System.out.println(point + "+" + (int) (money + 1 - 1) + "=" + (int) (point + money));
                        point = (int) (point + money);
                        System.out.println(Date());
                        System.out.println("此小票是您退换货及核对积分的凭证,请妥善保存!");
                        System.out.println(
                                "***************************************************************************************************");
                        // 购买列表清空
                        ca.clear();
                        return;
                    } else {
                        System.out.println("请输入正确的金额！");
                    }
                }
            } else {
                System.out.println("会员卡号错误！");
            }
        }
    }

    // 商品入库
    public void addin() {
        int num = -1;
        System.out.print("请输入商品编号：");
        String number = input.next();
        // 捕捉输入数量不正确的情况
        while (true) {
            try {
                Scanner inp = new Scanner(System.in);
                System.out.print("请输入入库数量：");
                num = inp.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("请输入整数");
                continue;
            }
        }
        // 去服务器端查询是否有此商品号,如果有则增加入库数量
        DataPool data = ca.addin(number, num);
        if (data.getFlag().equals(Flag.ADDIN)) {
            System.out.println("入库成功");
            // 输出所有商品信息
            System.out.println("商品编号\t\t商品名称\t\t\t商品单价\t\t单位\t\t数量");
            for (Commodity goods : data.getList()) {
                System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t\t" + goods.getGoodprice()
                        + "\t\t" + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());
            }
        } else {
            System.out.println("没有该商品，请选择编号3：新增商品");
            // 遍历输出商品信息
            System.out.println("商品编号\t\t商品名称\t\t\t商品单价\t\t单位\t\t数量");
            for (Commodity goods : data.getList()) {
                System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t\t" + goods.getGoodprice()
                        + "\t\t" + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());
            }
        }
    }

    // 商品出库
    public void out() {
        int num = -1;
        while (true) {
            System.out.print("请输入商品编号：");
            String number = input.next();
            // 捕捉输入数量不正确的情况
            while (true) {
                try {
                    Scanner inp = new Scanner(System.in);
                    System.out.print("请输入出库数量：");
                    num = inp.nextInt();
                    break;
                } catch (Exception e) {
                    System.out.println("请输入整数");
                    continue;
                }
            }
            DataPool data = ca.out(number, num);
            // 判断库存数量是否充足
            if (data.getFlag().equals(Flag.OUT)) {
                System.out.println("出库成功！");
                // 输出所有商品的信息
                System.out.println("商品编号\t\t商品名称\t\t商品单价\t\t单位\t\t数量");
                for (Commodity goods : data.getList()) {
                    System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t" + goods.getGoodprice()
                            + "\t\t" + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());
                }
                break;
            } else if (data.getFlag().equals(Flag.ERROR)) {
                System.out.println("库存不足，请核实，出库失败!");
                // 输出此商品的信息
                System.out.println("商品编号\t\t商品名称\t\t商品单价\t\t单位\t\t数量");
                System.out.println(data.getCommodity().getGoodNum() + "\t\t" + data.getCommodity().getGoodName()
                        + "\t\t" + data.getCommodity().getGoodprice() + "\t\t" + data.getCommodity().getGoodVolume()
                        + "\t\t" + data.getCommodity().getGoodCount());
                break;
            } else {
                System.out.println("没有此商品");
            }
        }
    }

    // 新增商品的方法
    public void newAdd() {
        // 判断新增的商品编号在库存中是否存在
        DataPool data;
        while (true) {
            System.out.print("请输入商品编号：");
            String number = input.next();
            // 查询商品是否存在，如果不存在则增加商品
            data = ca.search(number);
            if (data.getFlag().equals(Flag.SEARCH)) {
                System.out.print("请输入商品名称：");
                String name = input.next();
                System.out.print("请输入商品价格：");
                double price = input.nextDouble();
                System.out.print("请输入商品单位：");
                String unit = input.next();
                System.out.print("请输入商品数量：");
                int num = input.nextInt();
                Commodity commodity = new Commodity(number, name, price, unit, num);
                data = ca.newAdd(commodity);
                System.out.println("新增成功");
                // 输出所有的商品信息
                System.out.println("商品编号\t\t商品名称\t\t商品单价\t\t单位\t\t数量");
                for (Commodity goods : data.getList()) {
                    System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t" + goods.getGoodprice()
                            + "\t\t" + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());

                }
                break;
            } else {
                System.out.println("此编号已存在！");
            }
        }
    }

    // 查询所有商品
    public void allGoods() {
        DataPool data = ca.allGoods();
        // 输出所有的商品信息
        System.out.println("商品编号\t\t商品名称\t\t商品单价\t\t单位\t\t数量");
        for (Commodity goods : data.getList()) {
            System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t" + goods.getGoodprice()
                    + "\t\t" + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());

        }
    }

    // 按查询编号查询商品
    public void oneGoods() {
        System.out.print("请输入商品编号：");
        String number = input.next();
        DataPool data = ca.oneGoods(number);
        Commodity goods = data.getCommodity();
        // 输出单个商品信息
        System.out.println("商品编号\t\t商品名称\t\t商品单价\t\t单位\t\t数量");
        System.out.println(goods.getGoodNum() + "\t\t" + goods.getGoodName() + "\t\t" + goods.getGoodprice() + "\t\t"
                + goods.getGoodVolume() + "\t\t" + goods.getGoodCount());

    }
}